package cn.edu.hstc.wm.service;

import cn.edu.hstc.wm.AppConfigs;
import cn.edu.hstc.wm.Constants;
import cn.edu.hstc.wm.bean.User;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;

/**
 * @ClassName: AppConfigService
 * @Description: App 配置服务,读取数据
 * @ModificationHistory
 */
public class AppConfigService {

	/**
	 * 获取Preference设置
	 */
	public static SharedPreferences getDefSharedPreferences(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context);
	}

	/**
	 * 获取指定文件名的SharePreference
	 * 
	 * @param context
	 * @return
	 */
	public static SharedPreferences getSharedPreferences(Context context) {
		SharedPreferences preferences = context.getSharedPreferences(AppConfigs.APP_CONFIG, Context.MODE_PRIVATE);
		return preferences;
	}

	/**
	 * 获取SharePreference 的编辑对象 Editor
	 * 
	 * @param context
	 * @return
	 */
	public static Editor getEditor(Context context) {
		return getSharedPreferences(context).edit();
	}

	/**
	 * 存入String 类型
	 * 
	 * @param context
	 * @param key
	 * @param value
	 * @return
	 */
	public static boolean putStrVal2Conf(Context context, String key, String value) {
		return getEditor(context).putString(key, value).commit();
	}

	/**
	 * 存入Boolean 的类型
	 * 
	 * @param context
	 * @param key
	 * @param value
	 * @return
	 */
	public static boolean putBoolVal2Conf(Context context, String key, boolean value) {
		return getEditor(context).putBoolean(key, value).commit();
	}

	/**
	 * 保存用户信息到配置文件
	 * 
	 * @param context
	 * @param user
	 * @return
	 */
	public static boolean putUser2Conf(Context context, User user) {
		Editor editor = getEditor(context);
		editor.putString(Constants.USER_NAME, user.getName());
		// editor.putString(Constants.USER_PASSWD, user.getPassword());
		// editor.putBoolean(Constants.USER_IS_REMENBER_ME,
		// user.isRememberMe());
		editor.putString(Constants.USER_FACE, user.getFace());
		editor.putString(Constants.USER_GENDER, user.getGender());
		editor.putString(Constants.USER_REGISTER_TIME, user.getJointime());
		editor.putString(Constants.USER_LAST_ONLINE_TIME, user.getLatestonline());

		return editor.commit();
	}

	/**
	 * 从配置文件中取出User
	 * 
	 * @param context
	 * @return
	 */
	public static User getUserFromConf(Context context) throws Exception {
		User user = new User();
		if (TextUtils.isEmpty(getStrValFromConf(context, Constants.USER_NAME)) || TextUtils.isEmpty(getStrValFromConf(context, Constants.USER_PASSWD))) {
			return null;
		}
		user.setName(getStrValFromConf(context, Constants.USER_NAME));
		user.setPassword(getStrValFromConf(context, Constants.USER_PASSWD));
		user.setRememberMe(getBoolValFromConf(context, Constants.USER_IS_REMENBER_ME));
		user.setFace(getStrValFromConf(context, Constants.USER_FACE));
		user.setGender(getStrValFromConf(context, Constants.USER_GENDER));
		user.setJointime(getStrValFromConf(context, Constants.USER_REGISTER_TIME));
		user.setLatestonline(getStrValFromConf(context, Constants.USER_LAST_ONLINE_TIME));

		return user;
	}

	public static void clearUserInfo(Context context) {
		putStrVal2Conf(context, Constants.USER_NAME, "");
		putStrVal2Conf(context, Constants.USER_PASSWD, "");
		putBoolVal2Conf(context, Constants.USER_IS_REMENBER_ME, false);
		putStrVal2Conf(context, Constants.USER_FACE, "");

	}

	/**
	 * 根据key取得value
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	public static String getStrValFromConf(Context context, String key) {
		return getSharedPreferences(context).getString(key, "");
	}

	/**
	 * 根据key取得value
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	public static boolean getBoolValFromConf(Context context, String key) {
		return getSharedPreferences(context).getBoolean(key, true);
	}

	/** 是否从网络加载图片 */
	public static boolean isLoadImage(Context context) {
		return getDefSharedPreferences(context).getBoolean(Constants.LOAD_IMAGE, true);
	}

	/**
	 * 获取每页加载个数
	 */
	public static int getItemCount(Context context) {
		return Integer.parseInt(getDefSharedPreferences(context).getString(Constants.ITEM_COUNT, "8"));
	}

	/**
	 * 启动检查更新
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isStartCheckUpDate(Context context) {
		return getBoolValFromConf(context, Constants.START_CHECK_UPDATE);
	}

}
